// pages/shoppingBag/shoppingBag.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    shoppingList: [],
    cartList: [],
    show: true, // 购物车无商品使内容显隐
    footerNavShow: false, // 购物车底部结算显隐
    isClickedAll: true, // 全选控制
    totalPrice: 0, // 总价
    dialogShow: false // Dialog弹出框显隐
  },
  getData() {
    var _this = this;
    wx.request({
      url: 'https://www.fastmock.site/mock/942dfbb577a74c49a06b657043b1622a/lecake/lecakeShoppingBig',
      method: "GET",
      success(res) {
        _this.setData({
          shoppingList: res.data.data
        })
      }
    })
  },

  // 减少选中的商品数量 
  upNum(e) {
    let goodsId = e.target.dataset.index;
    app.globalData.cartList.forEach(item => {
      if (item.id == goodsId) {
        if (item.num > 1) {
          item.num--;
          app.globalData.cartList.num--
        } else if (item.num = 1) {
          this.data.cartList.splice((item.id - 1), 1);
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 1000
          });
        }
      }
    })
    this.setData({
      cartList: app.globalData.cartList
    })
    this.tabber();
    wx.setStorageSync('cartList', app.globalData.cartList);
    if (app.globalData.cartList.length == 0) {
      this.setData({
        show: true,
        footerNavShow: false
      })
    };
    this.totalPrice();
  },

  // 增加选中的商品数量 
  addNum(e) {
    let goodsId = e.target.dataset.index;
    app.globalData.cartList.forEach(item => {
      if (item.id == goodsId) {
        item.num++;
        app.globalData.cartList.num++
      }
    })
    this.setData({
      cartList: app.globalData.cartList
    })
    this.tabber();
    wx.setStorageSync('cartList', app.globalData.cartList);
    this.totalPrice();
  },

  // 导航栏下标更新
  tabber() {
    // 购物车下标（商品总数量）
    let num = 0;
    app.globalData.cartList.forEach(item => {
      num += item.num;
    })
    wx.setTabBarBadge({
      index: 3,
      text: '' + num + "",
    })
  },

  // 修改选择状态
  modifyIsChecked(e) {
    let goods = e.target.dataset.goods
    this.data.cartList.forEach(item => {
      if (item.id == goods.id) {
        item.isClicked = !item.isClicked;
      }
    })
    this.setData({
      cartList: this.data.cartList
    })
    app.globalData.cartList = this.data.cartList;
    wx.setStorageSync('cartList', this.data.cartList);
    this.judgeAll();
    this.totalPrice();
  },

  // 修改全选状态
  modifyIsCheckedAll() {
    let temp = !this.data.isClickedAll;
    this.judge() ? this.data.cartList.forEach(item => {
      item.isClicked = false
    }) : this.data.cartList.forEach(item => {
      item.isClicked = true
    });
    this.setData({
      isClickedAll: temp,
      cartList: app.globalData.cartList
    })
    wx.setStorageSync('cartList', this.data.cartList);
    this.totalPrice();
  },

  // 判断是否是处于全选状态
  judge() {
    return this.data.cartList.every((goods) => goods.isClicked);
  },
  judgeAll() {
    this.judge() ? this.setData({
      isClickedAll: true
    }) : this.setData({
      isClickedAll: false
    })
  },

  // 计算总价
  totalPrice() {
    let money = 0;
    this.data.cartList.forEach(item => {
      if (item.isClicked) {
        money += (item.money * item.num)
      }
    })
    this.setData({
      totalPrice: money
    })
  },

  // 判断
  judgeShow() {
    if (app.globalData.cartList.length == 0 || app.globalData.cartList == undefined) {
      this.setData({
        show: true,
        footerNavShow: false
      })
    } else {
      this.setData({
        show: false,
        footerNavShow: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.judgeShow();
    this.tabber();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
    this.judgeShow();
    // 获取全局变量的值，更新给data中的cartList变量
    this.setData({
      cartList: app.globalData.cartList
    });
    this.tabber();
    if (app.globalData.cartList != " ") {
      this.setData({
        show: false,
        footerNavShow: true
      })
    };
    this.judgeAll();
    this.totalPrice();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})